<?php   // $Id: devis_vers_commande.php 347 2012-05-07 09:11:55Z pcoustillas $ ?>
<?php   
/*Copyright (C) 2003-2004 Guy Hendrickx
Licensed under the terms of the GNU  General Public License:http://www.opensource.org/licenses/gpl-license.php
File Authors:Guy Hendrickx */
//	conversion des devis en bon de commande
require_once("include/verif.php");
require_once("include/config/common.php");
//require_once("include/head.php");


$num_dev=isset($_GET['num_dev'])?$_GET['num_dev']:"";


$jour = date("d");
$mois = date("m");
$annee = date("Y");
//on recpere les donnée de devis
$sql0 = "SELECT * FROM ${tblpref}devis WHERE num_dev = $num_dev";
$req = mysql_query($sql0) or die("Erreur selection du devis !<br/>$sql0<br/>" . mysql_error());

$data = mysql_fetch_array($req);

$num_dev = $data['num_dev'];
$client_num = $data['client_num'];
//$date = $data['date'];
$tot_htva = $data['tot_htva'];
$tot_tva = $data['tot_tva'];

//on les reinjecte dans la base bon_comm

$sql1 = "INSERT INTO ${tblpref}bon_comm ( client_num, date, tot_htva, tot_tva ) VALUES ( $client_num, '$annee-$mois-$jour', $tot_htva, $tot_tva )";

mysql_query($sql1) or die("Erreur création commande !<br/>$sql1<br/>".mysql_error());

$sql2 = "UPDATE ${tblpref}devis SET resu='ok' WHERE num_dev=$num_dev";
mysql_query($sql2) or die("Erreur mise à jour devis !<br/>$sql2<br/>".mysql_error());
//touver le dernier enregistrement pour le numero de bon
$sql = "SELECT MAX(num_bon) As Maxi FROM ${tblpref}bon_comm";
$result = mysql_query($sql) or die('Erreur');
$num_bon = mysql_result($result, 0);

$sql3 = "SELECT * FROM ${tblpref}cont_dev WHERE dev_num=$num_dev";
$req = mysql_query($sql3) or die("Erreur selection ligne de devis !<br/>$sql3<br/>".mysql_error());
while($data = mysql_fetch_array($req))
{
	$article_num = $data['article_num'];
	$quanti = $data['quanti'];
	$tot_art_htva = $data['tot_art_htva'];
	$to_tva_art = $data['to_tva_art'];
	//$p_u_jour = $data['p_u_jour'];
		
	$sql4 = "INSERT INTO ${tblpref}cont_bon(bon_num, article_num, quanti, tot_art_htva, to_tva_art) 
	VALUES ('$num_bon', '$article_num', '$quanti', '$tot_art_htva', '$to_tva_art')";
	mysql_query($sql4) or die("Erreur création bon de commande !<br/>$sql4<br/>".mysql_error());

	//on decremente le stock
	$sql12 = "UPDATE ${tblpref}article SET stock = (stock - $quanti) WHERE num='$article_num'";
	mysql_query($sql12) or die("Erreur mise à jour des stocks !<br/>$sql12<br/>".mysql_error());
}


header("Location: bon_edit.php?num_bon=$num_bon&message=Le bon de commande à été crée.");




?>
